package frame.options.physicalNetworks.create;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.event.DocumentEvent;
import javax.swing.event.DocumentListener;

import model.components.networks.factory.PhysicalNetworksFactory;
import com.nexes.wizard.WizardPanelDescriptor;

public class CreatePhysicalNetworksSelectDescriptor extends WizardPanelDescriptor implements DocumentListener, ActionListener
{
	public static final String IDENTIFIER = "CreatePhysicalNetworksDescriptor";
    
	private CreatePhysicalNetworksSelectPanel createPhysicalNetworksPanel;
	public CreatePhysicalNetworksSelectPanel getCreatePhysicalNetworksPanel(){ return createPhysicalNetworksPanel; }
	public void setCreatePhysicalNetworksPanel( CreatePhysicalNetworksSelectPanel createPhysicalNetworksPanel ){ this.createPhysicalNetworksPanel = createPhysicalNetworksPanel; }
	
	private CreatePhysicalNetworksWizard createPhysicalNetworksWizard;
	public CreatePhysicalNetworksWizard getCreatePhysicalNetworksWizard(){ return createPhysicalNetworksWizard; }
	public void setCreatePhysicalNetworksWizard( CreatePhysicalNetworksWizard createPhysicalNetworksWizard ){ this.createPhysicalNetworksWizard = createPhysicalNetworksWizard; }
	
	public CreatePhysicalNetworksSelectDescriptor()
    {
    	this.setCreatePhysicalNetworksPanel( new CreatePhysicalNetworksSelectPanel() );
    	this.getCreatePhysicalNetworksPanel().addListeners( this, this );
        this.setPanelDescriptorIdentifier( IDENTIFIER );
        this.setPanelComponent( this.getCreatePhysicalNetworksPanel() );
    }
    
	@Override
	public Object getBackPanelDescriptor()
    {
        return null;
    }
	@Override
	public Object getNextPanelDescriptor()
    {
        return CreatePhysicalNetworksFinishDescriptor.IDENTIFIER;
    }
	@Override
	public void aboutToDisplayPanel()
    {
    	this.setCreatePhysicalNetworksWizard( (CreatePhysicalNetworksWizard) this.getWizard() );
    	this.getCreatePhysicalNetworksWizard().setNextFinishButtonEnabled( false );
    	this.enableFinishButton();
    }
	public void aboutToHidePanel()
	{
		this.createPhysicalNetworks();
	}
	private void createPhysicalNetworks()
	{
		CreatePhysicalNetworksSelectPanel createPhysicalNetworksPanel = this.getCreatePhysicalNetworksPanel();
		
		int networksNumber = Integer.parseInt( createPhysicalNetworksPanel.getNetworksNumberTextField().getText() );
		
		int minNodesNumber = Integer.parseInt( createPhysicalNetworksPanel.getMinNodesNumberTextField().getText() );
    	int maxNodesNumber = Integer.parseInt( createPhysicalNetworksPanel.getMaxNodesNumberTextField().getText() );
    	
    	int routersPercentage  = Integer.parseInt( createPhysicalNetworksPanel.getRoutersPercentageTextField().getText() );
    	int switchesPercentage = Integer.parseInt( createPhysicalNetworksPanel.getSwitchesPercentageTextField().getText() );
		int serversPercentage  = Integer.parseInt( createPhysicalNetworksPanel.getServersPercentageTextField().getText() );
		
		int linuxPercentage = Integer.parseInt( createPhysicalNetworksPanel.getLinuxPercentageTextField().getText() );
		int windowsPercentage = Integer.parseInt( createPhysicalNetworksPanel.getWindowsPercentageTextField().getText() );
		int solarisPercentage = Integer.parseInt( createPhysicalNetworksPanel.getSolarisPercentageTextField().getText() );
		int androidPercentage = Integer.parseInt( createPhysicalNetworksPanel.getAndroidPercentageTextField().getText() );
		
		int vmWarePercentage = Integer.parseInt( createPhysicalNetworksPanel.getVmWarePercentageTextField().getText() );
		int xenPercentage = Integer.parseInt( createPhysicalNetworksPanel.getXenPercentageTextField().getText() );
		int kvmPercentage = Integer.parseInt( createPhysicalNetworksPanel.getKvmPercentageTextField().getText() );
		int umlPercentage = Integer.parseInt( createPhysicalNetworksPanel.getUmlPercentageTextField().getText() );
		
    	int minCpu = Integer.parseInt( createPhysicalNetworksPanel.getMinCpuTextField().getText() );
		int maxCpu = Integer.parseInt( createPhysicalNetworksPanel.getMaxCpuTextField().getText() );
		int minCpuUnitPrice = Integer.parseInt( createPhysicalNetworksPanel.getMinCpuUnitPriceTextField().getText() );
		int maxCpuUnitPrice = Integer.parseInt( createPhysicalNetworksPanel.getMaxCpuUnitPriceTextField().getText() );
		
		int minMemory = Integer.parseInt( createPhysicalNetworksPanel.getMinMemoryTextField().getText() );
		int maxMemory = Integer.parseInt( createPhysicalNetworksPanel.getMaxMemoryTextField().getText() );
		int minMemoryUnitPrice = Integer.parseInt( createPhysicalNetworksPanel.getMinMemoryUnitPriceTextField().getText() );
		int maxMemoryUnitPrice = Integer.parseInt( createPhysicalNetworksPanel.getMaxMemoryUnitPriceTextField().getText() );
		
		int minDiskSpace = Integer.parseInt( createPhysicalNetworksPanel.getMinDiskSpaceTextField().getText() );
		int maxDiskSpace = Integer.parseInt( createPhysicalNetworksPanel.getMaxDiskSpaceTextField().getText() );
		int minDiskSpaceUnitPrice = Integer.parseInt( createPhysicalNetworksPanel.getMinDiskSpaceUnitPriceTextField().getText() );
		int maxDiskSpaceUnitPrice = Integer.parseInt( createPhysicalNetworksPanel.getMaxDiskSpaceUnitPriceTextField().getText() );
		
		int maxVirtualNodesNumber = Integer.parseInt( createPhysicalNetworksPanel.getMaxVirtualNodesNumberTextField().getText() );
		
		int minBandwidth = Integer.parseInt( createPhysicalNetworksPanel.getMinBandwidthTextField().getText() );
		int maxBandwidth = Integer.parseInt( createPhysicalNetworksPanel.getMaxBandwidthTextField().getText() );
		int minBandwidthUnitPrice = Integer.parseInt( createPhysicalNetworksPanel.getMinBandwidthUnitPriceTextField().getText() );
		int maxBandwidthUnitPrice = Integer.parseInt( createPhysicalNetworksPanel.getMaxBandwidthUnitPriceTextField().getText() );
		
		int minDistance = Integer.parseInt( createPhysicalNetworksPanel.getMinDistanceTextField().getText() );
		int maxDistance = Integer.parseInt( createPhysicalNetworksPanel.getMaxDistanceTextField().getText() );
		
		PhysicalNetworksFactory physicalNetworksFactory = null;
		
		if( createPhysicalNetworksPanel.getLinksNumberPercentageRadioButton().isSelected() )
		{
			int linksNumberPercentage = Integer.parseInt( createPhysicalNetworksPanel.getLinksNumberPercentageTextField().getText() );
			physicalNetworksFactory = new PhysicalNetworksFactory( PhysicalNetworksFactory.NAME_PREFIX, networksNumber, minNodesNumber, maxNodesNumber, routersPercentage, switchesPercentage, serversPercentage, linuxPercentage, windowsPercentage, solarisPercentage, androidPercentage, vmWarePercentage, xenPercentage, kvmPercentage, umlPercentage, minCpu, maxCpu, minCpuUnitPrice, maxCpuUnitPrice, minMemory, maxMemory, minMemoryUnitPrice, maxMemoryUnitPrice, minDiskSpace, maxDiskSpace, minDiskSpaceUnitPrice, maxDiskSpaceUnitPrice, maxVirtualNodesNumber, linksNumberPercentage, minBandwidth, maxBandwidth, minBandwidthUnitPrice, maxBandwidthUnitPrice, minDistance, maxDistance );
		}
		else if( createPhysicalNetworksPanel.getLinksNumberRangeRadioButton().isSelected() )
		{
			int minLinksNumber = Integer.parseInt( createPhysicalNetworksPanel.getMinLinksNumberTextField().getText() );
			int maxLinksNumber = Integer.parseInt( createPhysicalNetworksPanel.getMaxLinksNumberTextField().getText() );
			physicalNetworksFactory = new PhysicalNetworksFactory( PhysicalNetworksFactory.NAME_PREFIX, networksNumber, minNodesNumber, maxNodesNumber, routersPercentage, switchesPercentage, serversPercentage, linuxPercentage, windowsPercentage, solarisPercentage, androidPercentage, vmWarePercentage, xenPercentage, kvmPercentage, umlPercentage, minCpu, maxCpu, minCpuUnitPrice, maxCpuUnitPrice, minMemory, maxMemory, minMemoryUnitPrice, maxMemoryUnitPrice, minDiskSpace, maxDiskSpace, minDiskSpaceUnitPrice, maxDiskSpaceUnitPrice, maxVirtualNodesNumber, minLinksNumber, maxLinksNumber, minBandwidth, maxBandwidth, minBandwidthUnitPrice, maxBandwidthUnitPrice, minDistance, maxDistance );
		}
		
		this.getCreatePhysicalNetworksWizard().setPhysicalNetworks( physicalNetworksFactory.getPhysicalNetworks() );
	}
	
	@Override
	public void actionPerformed( ActionEvent actionEvent )
	{
		this.getCreatePhysicalNetworksPanel().refreshLinksNumberRadioButton();
		this.enableFinishButton();
	}
	@Override
    public void changedUpdate( DocumentEvent documentEvent )
    {
    	this.enableFinishButton();
    }
	@Override
	public void insertUpdate( DocumentEvent documentEvent )
	{
		this.enableFinishButton();
	}
	@Override
	public void removeUpdate( DocumentEvent documentEvent )
	{
		this.enableFinishButton();
	}
	
	private void enableFinishButton()
	{
		if( this.getCreatePhysicalNetworksPanel().check() )
			this.getCreatePhysicalNetworksWizard().setNextFinishButtonEnabled( true );
		else
        	this.getCreatePhysicalNetworksWizard().setNextFinishButtonEnabled( false );
    }
	
}
